$.validator.addMethod(
  "telTest",
  function (value) {
    let reg = /1[3-9]{1}[0-9]{9}/g;
    return reg.test(value);
  },
  "手机号：不正确"
);

$.validator.addMethod(
  "pwdTest",
  function (value) {
    let reg = /^[a-zA-Z\d_]{6,}$/;
    return reg.test(value);
  },
  "长度至少为6,字母数字下划线"
);

$.validator.addMethod(
  "vcodeTest",
  function (value) {
    return captcha && captcha.validate(value);
  },
  "验证码不正确"
);

$(function () {
  captcha = new Captcha("#captcha", {
    refresh: false,
  });

  let eyeshow = false;
  $(".eye").on("click", function () {
    if (eyeshow) {
      $(".pwd").attr("type", "text");
      $(this).css("color", "#0B84FF");
    } else {
      $(".pwd").attr("type", "password");
      $(this).css("color", "rgb(0,0,0,0.5)");
    }
    eyeshow = !eyeshow;
  });
  let formv = $("form").validate({
    rules: {
      tel: {
        required: true,
        telTest: true,
      },
      password: {
        required: true,
        pwdTest: true,
      },
      vcode: {
        required: true,
        vcodeTest: true,
      },
    },
    // 当验证成功之后点击提交的时候 会执行的一个函数
    submitHandler: function (form) {},
    success: function (jq, el) {
      if (formv.checkForm()) {
        $(".submit").removeAttr("disabled").removeClass("disabled");
      }
    },
    errorElement: "div", //可以用其他标签，记住把样式也对应修改
    errorPlacement: function (error, element) {
      error.insertAfter(element.parent());
    },
    showErrors: function (errorMap, errorList) {
      this.defaultShowErrors();
      if (Object.keys(errorMap).length != 0 || errorList.length != 0) {
        $(".submit").attr("disabled", "disabled").addClass("disabled");
      }
    },
  });

  $(".submit").on("click", function () {
    $.pajax({
      url: "/api/reg.php",
      type: "post",
      dataType: "json",
      data: {
        tel: $("[name=tel]").val(),
        password: $("[name=password]").val(),
      },
    }).then((res) => {
      new Model({
        title: "信息",
        content: res.msg,
        showCancel: false,
        isDrag: true,
        skin: "tan" /* 主题色 "#20222A"|"#1E9FFF"|"tan" */,
        confirmCallbak: function () {
          this.close();
        },
      }).init();
      if (res.code == 1) {
        setTimeout(() => {
          location.href = "../html/login.html";
        }, 2000);
      }
    });
  });
});
